
//
//  FANHomeTableViewController.swift
//  Weibo - Swift
//
//  Created by user on 16/4/16.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

let reuseIdentifier = "reuseIdentifier"

class FANHomeTableViewController: FANVisitorController {

    lazy var statusViewModel:FANStatusViewModel = FANStatusViewModel()
    
    lazy var FRefreshControl:FANRefreshControl = FANRefreshControl()
    
    lazy var pullUpView :UIActivityIndicatorView = {
    
        let pullUpView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
        pullUpView.color = UIColor.darkGrayColor()
        
//        pullUpView.hidesWhenStopped = false
        
        return pullUpView
    
    }()
    
    lazy var pullDownTips:UILabel = {
    
        let label = UILabel()
        
        label.frame.size = CGSize(width: ScreenW, height: 35)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.textAlignment = .Center

        label.backgroundColor = UIColor.orangeColor()
        
        label.hidden = true
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        if !isLogin {
            
            visitorView.loadSomeInfo(nil, textTitle: nil)
            
            return
        }
        
        setupUI()
        
        loadData()
    }

    func setupUI(){
        
        tableView.registerClass(FANHomeCell.self, forCellReuseIdentifier: reuseIdentifier)
        
//        tableView.rowHeight = 150
        
        tableView.rowHeight = UITableViewAutomaticDimension
        
        tableView.estimatedRowHeight = 250
        
        tableView.backgroundColor = UIColor(white: 210/255, alpha: 1)
        
        tableView.separatorStyle = .None
        
        tableView.tableFooterView = pullUpView
        
        self.tableView.addSubview(FRefreshControl)
        
        FRefreshControl.addTarget(self, action: "loadData", forControlEvents:UIControlEvents.ValueChanged)
        
        self.navigationController!.view.addSubview(self.pullDownTips)
        
        self.navigationController!.view.insertSubview(pullDownTips, belowSubview: self.navigationController!.navigationBar)

    }
    
    @objc func loadData(){
        
        self.statusViewModel.loadData(pullUpView.isAnimating(), finished: { (success,count) -> () in
            
            if success {
                
                self.tableView.reloadData()
                
                
            }else{
                
                print("数据请求失败")
                
            }
            
            if self.pullUpView.isAnimating() == false{
                
                self.showPullDown(count)
            }
            
            self.endPullView()
            
        })
    }
    
    func showPullDown(count:Int){
        
        if pullDownTips.hidden == false{
            
            return
        }
        
        pullDownTips.hidden = false

        pullDownTips.text = count > 0 ? "\(count)条新微博" : "没有微博数据"
        
        pullDownTips.frame.origin.y = CGRectGetMaxY(self.navigationController!.navigationBar.frame) - pullDownTips.frame.size.height
        
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            self.pullDownTips.transform = CGAffineTransformMakeTranslation(0, self.pullDownTips.frame.size.height)
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.pullDownTips.transform = CGAffineTransformIdentity
                    
                    }, completion: { (_) -> Void in
                        
                        self.pullDownTips.hidden = true
                })
        }
        
    }
    
    func endPullView(){
        
        pullUpView.stopAnimating()
        
        FRefreshControl.endRefresh()
    }
    
}

extension FANHomeTableViewController {
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == ((statusViewModel.statusArray?.count)! - 1) ?? 0 && !pullUpView.isAnimating(){
            
            pullUpView.startAnimating()
            
            loadData()
        }
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return statusViewModel.statusArray?.count ?? 0
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as! FANHomeCell
        
//        let status = self.statusViewModel.statusArray![indexPath.row]
        cell.dataViewModel = statusViewModel.statusArray![indexPath.row]
        
        return cell
    }
}

